/**
 * <b>项目名称：</b>网络应用框架<br/>
 * <b>包    名：</b>com.hhwy.framework.util<br/>
 * <b>文 件 名：</b>PomUtil.java<br/>
 * <b>版本信息：</b><br/>
 * <b>日    期：</b>2016年9月18日-下午1:59:00<br/>
 * <b>Copyright (c)</b> 2016恒华伟业科技股份有限公司-版权所有<br/>
 * 
 */
package com.hhwy.framework.util;

import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.List;
import java.util.Map;

import org.apache.maven.model.Model;
import org.apache.maven.model.io.xpp3.MavenXpp3Reader;
import org.codehaus.plexus.util.xml.pull.XmlPullParserException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

 /**
 * <b>类 名 称：</b>PomUtil<br/>
 * <b>类 描 述：</b><br/>
 * <b>创 建 人：</b>liuhai<br/>
 * <b>修 改 人：</b>liuhai<br/>
 * <b>修改时间：</b>2016年9月18日 下午1:59:00<br/>
 * <b>修改备注：</b><br/>
 * @version 1.0.0<br/>
 */
public class PomUtil {
	private static boolean isPomXmlExist = false;
	private static String basePkgName = "";
	private static String groupId = "com.hhwy";
	private static String artifactId = "";
	private static String version = "0.0.1";
	private static String packaging = "jar";
	
	static Logger log = LoggerFactory.getLogger(PomUtil.class);
	static{
		 MavenXpp3Reader reader = new MavenXpp3Reader();
		 int index = IoTools.getABSPATH().indexOf("/src/");
		 File pomFile = null;
		 if(index != -1){
			 pomFile = new File(IoTools.getABSPATH().substring(0,index+1) + "pom.xml");
		 }else{
			 String metaInfDir = IoTools.getABSPATH() + "META-INF/";
			 Map<String, List<?>> map = new FileHelper().getFileList(metaInfDir, "pom.xml");
			 @SuppressWarnings("unchecked")
			 List<File> files = (List<File>) map.get(FileHelper.KEY_FILES);
			 if(files.size() > 0){
				 pomFile = files.get(0);
			 }
		 }
	     try {
	    	if(pomFile == null || !pomFile.isFile()){
	    		log.info("未发现pom.xml文件！");
	    		isPomXmlExist = false;
	    	}else{
	    		isPomXmlExist = true;
	    		Model model = reader.read(new FileReader(pomFile));
	    		basePkgName = model.getProperties().getProperty("project-base-package");
	    		if(model.getParent() != null){
	    			groupId = model.getParent().getGroupId();
	    			version = model.getParent().getVersion();
	    		}else{
	    			groupId = model.getGroupId();
	    			version = model.getVersion();
	    		}
	    		artifactId = model.getArtifactId();
	    		packaging = model.getPackaging();
	    		
	    		basePkgName = basePkgName.replace("${project.groupId}", groupId).replace("${project.artifactId}", artifactId);
	    	}
			
		} catch (IOException | XmlPullParserException e) {
			e.printStackTrace();
		}
	}
	public static boolean isPomXmlExist() {
		return isPomXmlExist;
	}
	public static String getBasePkgName() {
		return basePkgName;
	}
	public static String getGroupId() {
		return groupId;
	}
	public static String getArtifactId() {
		return artifactId;
	}
	public static String getVersion() {
		return version;
	}
	public static String getPackaging() {
		return packaging;
	}
	
}
